#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>

int main()
{
    char * const myargv[]={
        //"ls",
        //"-a",
        //"-l",
        //NULL
        "other",
        "-a",
        "-b",
        "-c",
        NULL
    };
    extern char** environ;
    pid_t id = fork();

    if(id == 0){
        printf("before:I am a process,pid:%d,ppid:%d\n",getpid(),getppid());
        //execl("/usr/bin/ls","ls","-l","-a",NULL);
        //execlp("ls","ls","-l","-a",NULL);//会根据环境变量PATH下去寻找对应文件
        //execv("/usr/bin/ls",myargv);
        //execvp("ls",myargv);
        //execv("./other",myargv);
        //execle("./other","other","-w","-v","-a",NULL,environ);
        char* const myenv[]={
            "MYVALUE=12345",
            "MYTYPE=99999",
            NULL
        };
        execle("./other","other","-w","-v","-a",NULL,myenv);
        //execl("/usr/bin/top","top",NULL);
        printf("after:I am a process,pid:%d,ppid:%d\n",getpid(),getppid());
        exit(1);
    }
    else{
        int status = 0;
        pid_t ret = waitpid(id,&status,0);
        if(ret == id){
            printf("wait success,pid:%d\n",getpid());
        }
        else{
            printf("faild wait,pid:%d,exit code:%d\n",getpid(),WEXITSTATUS(status));
        }
    }
    return 0;
}
